<?php

use Illuminate\Database\Seeder;
use Encore\Admin\Auth\Database\Menu;
use Encore\Admin\Auth\Database\Role;
use Modules\Merchant\Entities\Staff;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Auth\Database\Permission;

class AdminSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->user();
        $this->role();

        // add role to user.
        Staff::find(1)->roles()->save(Role::find(1));
        Staff::find(2)->roles()->save(Role::find(2));

        $this->permission();

        Role::first()->permissions()->save(Permission::first());

        $this->menu();

        // add role to menu.
        // Menu::find(5)->roles()->save(Role::first());
        
    }

    protected function user()
    {
        Staff::truncate();
        Staff::insert([
            [
                'username' => 'admin',
                'password' => bcrypt('admin'),
                'name'     => 'Administrator',
                'merchant_id' => 0,
            ],
            [
                'username' => 'boss',
                'password' => bcrypt('boss'),
                'name'     => 'Boss',
                'merchant_id' => 1,
            ],
        ]); 
    }

    protected function role()
    {
        // create a role.
        Role::truncate();
        Role::insert([
            [
                'name' => 'Administrator',
                'slug' => 'administrator',
            ],
            [
                'name' => 'Boss',
                'slug' => 'boss',
            ],
        ]);
    }

    protected function permission()
    {
        //create a permission
        Permission::truncate();
        Permission::insert([
            [
                'name'        => 'All permission',
                'slug'        => '*',
                'http_method' => '',
                'http_path'   => '*',
            ],
            [
                'name'        => 'Dashboard',
                'slug'        => 'dashboard',
                'http_method' => 'GET',
                'http_path'   => '/',
            ],
            [
                'name'        => 'Login',
                'slug'        => 'auth.login',
                'http_method' => '',
                'http_path'   => "/auth/login\r\n/auth/logout",
            ],
            [
                'name'        => 'User setting',
                'slug'        => 'auth.setting',
                'http_method' => 'GET,PUT',
                'http_path'   => '/auth/setting',
            ],
            [
                'name'        => 'Auth management',
                'slug'        => 'auth.management',
                'http_method' => '',
                'http_path'   => "/auth/roles\r\n/auth/permissions\r\n/auth/menu\r\n/auth/logs",
            ],
        ]);
    }

    protected function menu()
    {
        $data = [
            [
                'title'     => '首页',
                'icon'      => 'fa-bar-chart',
                'uri'       => '/',
            ],

            [
                'title'     => '商品',
                'icon'      => 'fa-cubes',
                'uri'       => '',
                'sub'       => [
                    [
                        'title'     => '分类管理',
                        'icon'      => 'fa-bars',
                        'uri'       => 'category',
                    ],
                    [
                        'title'     => '商品管理',
                        'icon'      => 'fa-barcode',
                        'uri'       => 'goods',
                    ],
                ]
            ],

            [
                'title'     => '销售',
                'icon'      => 'fa-list-alt',
                'uri'       => '',
                'sub'       => [
                    [
                        'title'     => '订单',
                        'icon'      => 'fa-shopping-cart',
                        'uri'       => 'order',
                    ],
                    [
                        'title'     => '采购',
                        'icon'      => 'fa-cart-plus',
                        'uri'       => 'purchase',
                    ],
                    [
                        'title'     => '入库',
                        'icon'      => 'fa-cart-arrow-down',
                        'uri'       => 'storage',
                    ],
                ]
            ],

            [
                'title'     => '营销',
                'icon'      => 'fa-list-alt',
                'uri'       => '',
                'sub'       => [
                    [
                        'title'     => '营销活动管理',
                        'icon'      => 'fa-shopping-cart',
                        'uri'       => 'coupon/active',
                    ],
                    [
                        'title'     => '优惠卷管理',
                        'icon'      => 'fa-cart-plus',
                        'uri'       => 'purchase',
                    ],
                ]
            ],           

            [
                'title'     => '商家管理',
                'icon'      => 'fa-list-alt',
                'uri'       => '',
                'sub'       => [
                    [
                        'title'     => '商家信息',
                        'icon'      => 'fa-shopping-cart',
                        'uri'       => 'merchant',
                    ],
                    [
                        'title'     => '员工',
                        'icon'      => 'fa-cart-plus',
                        'uri'       => 'merchant',
                    ],
                ]
            ],

            [
                'title'     => '系统管理',
                'icon'      => 'fa-tasks',
                'uri'       => '',
                'sub'       => [
                    [
                        'title'     => 'Users',
                        'icon'      => 'fa-users',
                        'uri'       => 'auth/users',
                    ],
                    [
                        'title'     => 'Roles',
                        'icon'      => 'fa-user',
                        'uri'       => 'auth/roles',
                    ],
                    [
                        'title'     => 'Permission',
                        'icon'      => 'fa-ban',
                        'uri'       => 'auth/permissions',
                    ],
                    [
                        'title'     => 'Menu',
                        'icon'      => 'fa-bars',
                        'uri'       => 'auth/menu',
                    ],
                    [
                        'title'     => 'Operation log',
                        'icon'      => 'fa-history',
                        'uri'       => 'auth/logs',
                    ],
                ]
            ],

        ];

        // add default menus.
        Menu::truncate();

        $order = 1;
        foreach ($data as $menu) {
            $sub = [];

            if (array_key_exists("sub", $menu)) {
                $sub = $menu['sub'];
                unset($menu['sub']);
            }

            $menu['order'] = $order++;
            $parent = Menu::insertGetId($menu);

            // print_r($sub);
            foreach ($sub as $item) {
                $item['parent_id'] = $parent;
                $item['order'] = $order++;
                Menu::insert($item);
            }
        }

    }
}
